package org.andromeo.connectionapi;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.andromeo.ARException;
import org.andromeo.GRSession;
import org.andromeo.User;
import org.andromeo.UserFactory;
import org.andromeo.util.ARRequester;

public class VisitorsListProcessor {

	private static final Pattern visitorlinePattern = Pattern
			.compile(
					"<tr.*?colspan=\"6\".*?<tr.*?set=.*?<tr.*?<tr.*?colspan=\"5\".*?</tr>",
					Pattern.DOTALL);

	private static final Pattern usernamePattern = Pattern.compile(
			"<a.*?set=(\\d*).*?>(\\w+?)</a>", Pattern.DOTALL);

	// 13.05.-21:41
	private static final Pattern datetimePattern = Pattern.compile(
			"\\d\\d\\.\\d\\d\\.-\\d\\d\\:\\d\\d", Pattern.DOTALL);

	private static final SimpleDateFormat sdtf = new SimpleDateFormat(
			"dd.MM.-HH:mm");

	/*
	 * group(1) : contains the online class group(2) : case group(1) contains
	 * "isnotonline" you find a date here
	 */
	private static final Pattern spanPattern = Pattern.compile(
			"<span.*?>(.*?)</span>", Pattern.DOTALL);

	/*
	 * group(1) contains footprint url
	 */
	private static final Pattern footprintimgPattern = Pattern.compile(
			"src=\"([\\w/\\.]*?footprints[\\w/\\.]*?)\"", Pattern.DOTALL);

	/*
	 * group(1) contains thumbnail url
	 */
	private static final Pattern thumbnailimgPattern = Pattern.compile(
			"src=\"(.*?)\".*?class=\"thumb\"\\s/>", Pattern.DOTALL);

	public static synchronized Vector<User> getVisitorList(GRSession grsession)
			throws ARException, IOException {
		Vector<User> userlist = new Vector<User>();

		UserFactory userfab = grsession.getUserFactory();

		String urlstring = grsession.getGrurl();
		urlstring += grsession.getPhpSessionID();
		urlstring += "/search/index.php?action=execute&searchType=myVisitors";

		String response = ARRequester.sendRequest(grsession, urlstring, "");

		Matcher visitorlineMatcher = visitorlinePattern.matcher(response);

		while (visitorlineMatcher.find()) {

			String line = visitorlineMatcher.group();

			String uname = null;
			String uid = null;
			String datestring = null;
			Date date = new Date();
			String footprinturl = null;
			String thumbnailurl = null;
			String onlinestatus = null;
			String headline = null;

			Matcher usernameMatcher = usernamePattern.matcher(line);
			Matcher datetimeMatcher = datetimePattern.matcher(line);
			Matcher spanMatcher = spanPattern.matcher(line);
			Matcher footprintimgMatcher = footprintimgPattern.matcher(line);
			Matcher thumbnailimgMatcher = thumbnailimgPattern.matcher(line);

			if (usernameMatcher.find()) {
				uid = usernameMatcher.group(1);
				uname = usernameMatcher.group(2);

				if (datetimeMatcher.find()) {
					datestring = datetimeMatcher.group();
					try {
						date = sdtf.parse(datestring);
					} catch (ParseException e) {
						e.printStackTrace();
					}
				}

				if (footprintimgMatcher.find()) {
					footprinturl = grsession.getGrurl()
							+ footprintimgMatcher.group(1);
				}

				if (thumbnailimgMatcher.find()) {
					thumbnailurl = thumbnailimgMatcher.group(1);
				}

				if (spanMatcher.find()) {
					// first span-tag
					onlinestatus = spanMatcher.group(1);
					// second span-tag
					if (spanMatcher.find())
						headline = spanMatcher.group(1);
				}

				User u = userfab.getUserById(uid);
				u.setUserName(uname);
				u.setRecFootprintURL(footprinturl);
				u.setRecFootprintTitle("XXX");
				u.setThumbnailImageURL(thumbnailurl);
				u.setOnlineStatus(onlinestatus);
				userlist.add(u);
			}

		}

		return userlist;
	}

}